package top.v5it.japi.ddd.example.account.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.v5it.japi.ddd.core.service.FileService;
import top.v5it.japi.ddd.core.web.HttpHeadFactory;
import top.v5it.japi.ddd.core.web.TraceContextHolder;

import java.io.File;
import java.util.Map;
import java.util.Optional;

/**
 * @author zhanpu
 * @date 2023/4/14
 */
@Slf4j
@Service("testFileService")
public class TestFileServiceImpl implements FileService {

    @Override
    public Object processor(File file, Map<String, Object> params) {
        HttpHeadFactory.INSTANCE.getRequestHeader("token").ifPresent(token -> log.info("token:{}", token));
        log.info(file.getAbsolutePath());
        log.info("{}", params);
        HttpHeadFactory.INSTANCE.putResponseHead("Response-ID", IdUtil.fastSimpleUUID());

        final String spanId = Optional.ofNullable(TraceContextHolder.getSpanId())
                .filter(StrUtil::isNotBlank)
                .orElse(TraceContextHolder.rootSpanId());

        final String traceId = Optional.ofNullable(TraceContextHolder.getTraceId())
                .filter(StrUtil::isNotBlank)
                .orElse(TraceContextHolder.computeTraceId());

        log.info("traceId: {}, spanId: {}", traceId, spanId);
        return "OK";
    }
}
